.PHONY: clean boot
# 定义变量
BUILD_DIR=./os/build
HD60M_PATH=/home/bon/test/linux/learnlinux/hd60m.img
ENTRY_POINT=0xc0001500
AS=nasm
CC=gcc
LD=ld
INC= -I os/lib/ -I os/lib/kernel -I os/lib/usr -I os/kernel \
     -I os/dev -I os/thread -I os/userprog -I os/fs -I os/shell
BOOT_INC = -I os/boot
ASFLAGS = -f elf
CFLAGS = -Wall ${INC} -c -fno-builtin -W -m32 
CFLAGS2 = -Wall ${INC} -c -fno-builtin -W -m32 -Wstrict-prototypes -Wmissing-prototypes -Wsystem-headers
LDFLAGS = -Ttext ${ENTRY_POINT} -e main -m elf_i386
LDFLAGS2 = -e main -m elf_i386
OBJS=${BUILD_DIR}/main.o ${BUILD_DIR}/init.o ${BUILD_DIR}/interrupt.o \
   ${BUILD_DIR}/timer.o ${BUILD_DIR}/print.o ${BUILD_DIR}/kernel.o  \
   ${BUILD_DIR}/debug.o ${BUILD_DIR}/string.o ${BUILD_DIR}/bitmap.o  \
   ${BUILD_DIR}/memory.o ${BUILD_DIR}/thread.o ${BUILD_DIR}/switch.o \
   ${BUILD_DIR}/list.o ${BUILD_DIR}/sync.o ${BUILD_DIR}/console.o  \
   ${BUILD_DIR}/keyboard.o ${BUILD_DIR}/ioqueue.o ${BUILD_DIR}/tss.o \
   ${BUILD_DIR}/process.o ${BUILD_DIR}/syscall.o ${BUILD_DIR}/syscall_init.o \
   ${BUILD_DIR}/stdio.o ${BUILD_DIR}/stdio_kernel.o ${BUILD_DIR}/ide.o \
   ${BUILD_DIR}/fs.o ${BUILD_DIR}/dir.o ${BUILD_DIR}/file.o ${BUILD_DIR}/inode.o \
   ${BUILD_DIR}/fork.o ${BUILD_DIR}/assert.o ${BUILD_DIR}/shell.o\
   ${BUILD_DIR}/buildin_cmd.o ${BUILD_DIR}/exec.o ${BUILD_DIR}/wait_exit.o 

OBJS2=${BUILD_DIR}/print.o
# 编译
main.o: os/kernel/main.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/main.o $< 
init.o: os/kernel/init.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/init.o $<
timer.o: os/dev/timer.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/timer.o $<
interrupt.o: os/kernel/interrupt.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/interrupt.o $<
debug.o: os/kernel/debug.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/debug.o $<
string.o: os/lib/string.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/string.o $<
bitmap.o: os/lib/kernel/bitmap.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/bitmap.o $<
memory.o: os/kernel/memory.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/memory.o $<
thread.o: os/thread/thread.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/thread.o $<
list.o: os/lib/kernel/list.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/list.o $<
sync.o: os/thread/sync.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/sync.o $<
console.o: os/dev/console.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/console.o $<
keyboard.o: os/dev/keyboard.c os/dev/ioqueue.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/keyboard.o $< 
ioqueue.o: os/dev/ioqueue.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/ioqueue.o $<
tss.o: os/userprog/tss.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/tss.o $<
process.o: os/userprog/process.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/process.o $<
syscall.o: os/lib/usr/syscall.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/syscall.o $<
syscall_init.o: os/userprog/syscall_init.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/syscall_init.o $<
stdio.o: os/lib/stdio.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/stdio.o $<
stdio_kernel.o: os/lib/kernel/stdio_kernel.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/stdio_kernel.o $<
ide.o: os/dev/ide.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/ide.o $<
fs.o: os/fs/fs.c os/fs/fs.h os/fs/inode.h os/fs/super_block.h os/fs/dir.h
	${CC} ${CFLAGS} -o ${BUILD_DIR}/fs.o $<
dir.o: os/fs/dir.c 
	${CC} ${CFLAGS} -o ${BUILD_DIR}/dir.o $< 
file.o: os/fs/file.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/file.o $<
inode.o: os/fs/inode.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/inode.o $<
fork.o: os/userprog/fork.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/fork.o $<
shell.o: os/shell/shell.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/shell.o $<
buildin_cmd.o: os/shell/buildin_cmd.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/buildin_cmd.o $< 
assert.o: os/lib/usr/assert.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/assert.o $<
exec.o: os/userprog/exec.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/exec.o $<
wait_exit.o: os/userprog/wait_exit.c
	${CC} ${CFLAGS} -o ${BUILD_DIR}/wait_exit.o $<

switch.o: os/thread/switch.S
	${AS} ${ASFLAGS} -o ${BUILD_DIR}/switch.o $<
print.o: os/lib/kernel/print.S
	${AS} ${ASFLAGS} -o ${BUILD_DIR}/print.o $<
kernel.o: os/kernel/kernel.S
	${AS} ${ASFLAGS} -o ${BUILD_DIR}/kernel.o $<
mbr.bin: os/boot/mbr.S
	${AS} ${BOOT_INC} -o ${BUILD_DIR}/mbr.bin $<
loader.bin: os/boot/loader.S
	${AS} ${BOOT_INC} -o ${BUILD_DIR}/loader.bin $<
kernel.bin: shell.o main.o init.o interrupt.o timer.o print.o kernel.o debug.o \
            string.o bitmap.o memory.o thread.o list.o switch.o sync.o \
            console.o ioqueue.o keyboard.o tss.o process.o syscall.o \
			syscall_init.o stdio.o stdio_kernel.o ide.o fs.o inode.o \
			file.o dir.o fork.o assert.o buildin_cmd.o exec.o wait_exit.o
	${LD} ${LDFLAGS} -o ${BUILD_DIR}/kernel.bin ${OBJS}

boot: kernel.bin mbr.bin loader.bin 
	dd if=${BUILD_DIR}/mbr.bin of=${HD60M_PATH} bs=512 count=1 conv=notrunc
# seek=2， 储存在第3个扇区上
	dd if=${BUILD_DIR}/loader.bin of=${HD60M_PATH} bs=512 count=4 seek=2 conv=notrunc
	dd if=${BUILD_DIR}/kernel.bin of=${HD60M_PATH} bs=512 count=200 seek=9 conv=notrunc

clean:
	rm -f ${BUILD_DIR}/*
